<?php

/*
    user_id 评论者的id
        我们在这 给一个固定值 1
    article_id 要评论的文章的id
    content 评论的内容
*/

$article_id = $_POST["article_id"];
$content = $_POST['content'];
include "./mysql.php";

$insert_sql = "INSERT INTO t_comments (user_id,article_id,content) VALUES (1,{$article_id},'{$content}')";

$result = $mysqli->query($insert_sql);

$insert_id = $mysqli->insert_id;

// 要再写一个sql语句，查出刚才添加到数据库的那个评论 ，根据$insert_id

$comment_sql = "SELECT id,user_id,article_id,content as detail,created_time as postdate ,(SELECT nickname FROM t_users WHERE id= t_comments.user_id) as user ,(SELECT avatar FROM t_users WHERE id= t_comments.user_id) as user_avatar FROM t_comments WHERE id = {$insert_id}";

$res = $mysqli->query($comment_sql);

$row = $res->fetch_assoc();

echo (json_encode($row));

//更改的

// if ($mysqli->affected_rows) {
//     // 代表删除成功
//     $arr = array("code" => 200, "msg" => "删除成功");
// } else {
//     // 代表删除失败
//     $arr = array("code" => 404, "msg" => "删除失败");
// }

// echo (json_encode($arr));
